/**
 * jims
 */
package com.jims.master.modules.oper.service;


import com.jims.master.common.config.Global;
import com.jims.master.common.persistence.BaseEntity;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.oper.dao.ClinicItemDictDao;
import com.jims.master.modules.oper.dao.ClinicVsChargeDao;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.oper.dao.PriceListLogDao;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.entity.ClinicVsCharge;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.oper.entity.PriceListLog;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 价表Service
 *
 * @author CTQ
 * @version 2017-02-22
 */
@Service
@Transactional(readOnly = true)
public class PriceListService extends CrudService<PriceListDao, PriceList> {
    @Autowired
    private PriceListDao priceListDao;
    @Autowired
    private ClinicItemDictDao clinicItemDictDao;
    @Autowired
    private ClinicVsChargeDao clinicVsChargeDao;
    @Autowired
    private PriceListLogDao priceListLogDao;


    /**
     * 查询价表数据
     * @param priceList
     * @return
     */
    public List<BaseDto> findListDto(PriceList priceList){
        return dao.findListDto(priceList);
    }

    /**
     * 保存价表信息
     * @param priceList
     * @return
     * @author CTQ
     */
    @Transactional(readOnly = false)
    public int savePriceList(PriceList priceList) {
        int num = 0;
        if (priceList != null && StringUtils.isNotBlank(priceList.getId())) {
            if (StringUtils.isNotBlank(priceList.getItemName())) {
                priceList.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(priceList.getItemName()));
            }
            priceList.preUpdate();
            priceList.updateOrgId();
//			getMaxItemCode(priceList);
            num = priceListDao.update(priceList);
            /**查询该价表项目是否有对照及诊疗项目，如果有则更新诊疗项目**/
//            ClinicItemDict itemDict = clinicItemDictDao.findByParams(priceList.getId(), priceList.getOrgId());
//            if (itemDict != null) {
//                itemDict.setItemClass(priceList.getItemClass());
//                String itemCode = "I" + priceList.getItemCode().substring(priceList.getItemCode().indexOf(priceList.getItemCode()) + 1);
//                itemDict.setItemCode(itemCode);
//                itemDict.setItemName(priceList.getItemName());
//                itemDict.setInputCode(priceList.getInputCode());
//                itemDict.preUpdate();
//                clinicItemDictDao.update(itemDict);
//            }
        } else {
            if (StringUtils.isNotBlank(priceList.getItemName())) {
                priceList.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(priceList.getItemName()));
            }
            priceList.updateOrgId();
            getMaxItemCode(priceList);
            priceList.preInsert();

            num = priceListDao.insert(priceList);
            pubMethod(priceList);
            if (priceList.getMark() != null && priceList.getMark().equals("1")) {//生成诊疗项目及对照
                ClinicItemDict itemDict = new ClinicItemDict();
                itemDict.setOrgId(UserUtils.getOrgId());
                itemDict.setItemClass(priceList.getItemClass());
                String itemCode = "I" + priceList.getItemCode().substring(priceList.getItemCode().indexOf(priceList.getItemCode()) + 1);
                itemDict.setItemCode(itemCode);
                itemDict.setItemName(priceList.getItemName());
                itemDict.setInputCode(priceList.getInputCode());
                itemDict.preInsert();
                clinicItemDictDao.insert(itemDict);
                ClinicVsCharge clinicVsCharge = new ClinicVsCharge();
                clinicVsCharge.setChargeId(priceList.getId());
                clinicVsCharge.setClinicId(itemDict.getId());
                clinicVsCharge.setAmount("1");
                clinicVsCharge.preInsert();
                clinicVsChargeDao.insert(clinicVsCharge);
            }

        }
        return num;
    }

    /**
     * 生成最大itemCode号
     *
     * @param priceList
     */
    public void getMaxItemCode(PriceList priceList) {

        int maxItemCode = priceListDao.getMaxItemCode(priceList.getOrgId(), priceList.getItemClass());
        String itemCode = "";
        if (maxItemCode == 0) {
            itemCode = "P" + priceList.getItemClass() + "1";
        } else {
            itemCode = "P" + priceList.getItemClass() + (maxItemCode + 1);
        }
        priceList.setItemCode(itemCode);
    }


    /**
     * 价表调价
     * @author CTQ
     * @param priceList
     * @return
     */
    @Transactional(readOnly = false)
    public int updatePriceList(PriceList priceList) {
        int num = 0;
        if (priceList != null && priceList.getId() != null && !priceList.getId().equals("")) {
            /**1.更新price_list_log中id=此记录ID and is_ner='1' and stop_date is null的stop_date和is_ner=0**/
            priceListLogDao.updateByPriceId(priceList.getId(), Global.NO, priceList.getStartDate());
            /**2.重新插入一条新启用价表记录**/
            PriceList price = priceListDao.get(priceList.getId());
            price.setPrice(priceList.getPrice());
            price.setPreferPrice(priceList.getPreferPrice());
            price.setStartDate(priceList.getStartDate());
            pubMethod(price);
            /**更新价表**/
            price.preUpdate();
            num = priceListDao.update(price);
        }
        return num;
    }

    /**
     * 价表新增与调价均插入记录表
     * @author CTQ
     * @param priceList
     */
    public int pubMethod(PriceList priceList) {
        PriceListLog log = new PriceListLog();
        log.setOrgId(priceList.getOrgId());
        log.setInputCode(priceList.getInputCode());
        log.setClassOnMr(priceList.getClassOnMr());
        log.setClassOnRcpt(priceList.getClassOnRcpt());
        log.setClassOnReckoning(priceList.getClassOnReckoning());
        log.setIsNer(Global.YES);
        log.setItemClass(priceList.getItemClass());
        log.setItemCode(priceList.getItemCode());
        log.setItemName(priceList.getItemName());
        log.setItemSpec(priceList.getItemSpec());
        log.setPreferPrice(priceList.getPreferPrice());
        log.setPrice(priceList.getPrice());
        log.setPriceId(priceList.getId());
        log.setStartDate(priceList.getStartDate());
        log.setStopDate(priceList.getStopDate());
        log.setSubjCode(priceList.getSubjCode());
        log.setUnits(priceList.getUnits());
        log.preInsert();
        return priceListLogDao.insert(log);
    }

    /**
     * 停价
     * @author CTQ
     * @param priceList
     * @return
     */
    @Transactional(readOnly = false)
    public int stopPriceList(PriceList priceList) {
        int num = 0;
        Date date = new Date();
        if (priceList.getStartDate().after(date)){
            priceList.setStartDate(date);
        }
        priceList.setStopDate(date);
        priceList.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
        /**1.停掉调价记录数据**/
        priceListLogDao.updateByPriceId(priceList.getId(), Global.NO, priceList.getStopDate());
        /**停用价表数据**/
        priceList.preUpdate();
        num = priceListDao.update(priceList);

        return num;
    }

    /**
     * 启用价表
     * @param priceList
     * @return
     */
    @Transactional(readOnly = false)
    public int openPriceList(PriceList priceList) {
        int num = 0;
        priceList.setStopDate(null);
        priceList.setDelFlag(BaseEntity.DEL_FLAG_NORMAL);
        /**1.启用调价记录数据**/
        PriceListLog log = priceListLogDao.findLog(priceList.getId());
        log.setStopDate(null);
        log.preUpdate();
        priceListLogDao.update(log);
        /**启用价表数据**/
        priceList.preUpdate();
        num = priceListDao.updateById(priceList);

        return num;
    }


    /**
     * 诊疗项目添加对照，查询价表
     *
     * @param clinicId
     * @param q,clinicId,limit,type
     * @param type
     * @return
     */
    public List<PriceList> findListByParam(String q, String clinicId, String limit, String type) {
        return priceListDao.findListByParam(q, clinicId, limit, type);
    }

    /**
     * 费别查询价表
     * @param q
     * @param clinicId
     * @param limit
     * @param type
     * @param chargeType
     * @return
     * @author 赵宁
     */
    public List<PriceList> findListByCharge(String q, String clinicId, String limit, String type,String chargeType) {
        return priceListDao.findListByCharge(q, clinicId, limit, type,chargeType);
    }

    /**
     * 通过诊疗项目查询价表项目
     *
     * @param clinicId
     * @return
     */
    public List<PriceList> findListPrice(String clinicId) {
        return priceListDao.findListPrice(clinicId);
    }

}